<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>线段树模板 | 探花需拔根</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
    <meta name="keywords" content="Bluarry,bluarry,Blog,探花,拔根,博客,区块链,算法,acm">
  
  <meta name="description" content="线段树模板">
<meta name="keywords" content="学习,acm,算法">
<meta property="og:type" content="article">
<meta property="og:title" content="线段树模板">
<meta property="og:url" content="/2017/08/29/2017-08-29-线段树模板/index.html">
<meta property="og:site_name" content="探花需拔根">
<meta property="og:description" content="线段树模板">
<meta property="og:locale" content="zh.yml">
<meta property="og:image" content="https://cdn.bluarry.top/wp-content/uploads/2019/12/sg1.jpg">
<meta property="og:image" content="https://cdn.bluarry.top/wp-content/uploads/2019/12/sg2.jpg">
<meta property="og:image" content="https://cdn.bluarry.top/wp-content/uploads/2019/12/sg3.jpg">
<meta property="og:image" content="https://cdn.bluarry.top/wp-content/uploads/2019/12/sg4.jpg">
<meta property="og:updated_time" content="2020-03-02T12:53:47.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="线段树模板">
<meta name="twitter:description" content="线段树模板">
<meta name="twitter:image" content="https://cdn.bluarry.top/wp-content/uploads/2019/12/sg1.jpg">
  
  
    <link rel="icon" href="/favicon.ico">
  
  <link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css">
  <script src="/js/pace.min.js"></script>
  

  
  

  
<!-- Matomo -->
<script type="text/javascript">
	var _paq = window._paq || [];
	/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
	_paq.push(['trackPageView']);
	_paq.push(['enableLinkTracking']);
	(function() {
	  var u="//matomo.bluarry.top/";
	  _paq.push(['setTrackerUrl', u+'matomo.php']);
	  _paq.push(['setSiteId', '3']);
	  var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
	  g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
	})();
  </script>
  <!-- End Matomo Code -->

</head>
</html>
<body>
  <div id="container">
      <header id="header">
    <div id="banner"></div>
    <div id="header-outer">
        <div id="header-menu" class="header-menu-pos animated">
            <div class="header-menu-container">
                <a href="/" class="left">
                    <span class="site-title">探花需拔根</span>
                </a>
                <nav id="header-menu-nav" class="right">
                    
                    <a  href="/">
                        <i class="fa fa-home"></i>
                        <span>主页</span>
                    </a>
                    
                    <a  href="/archives">
                        <i class="fa fa-archive"></i>
                        <span>归档</span>
                    </a>
                    
                    <a  href="/friends">
                        <i class="fa fa-envira"></i>
                        <span>友链</span>
                    </a>
                    
                    <a  href="/about">
                        <i class="fa fa-user"></i>
                        <span>关于我</span>
                    </a>
                    
                </nav>
                <a class="mobile-header-menu-button">
                    <i class="fa fa-bars"></i>
                </a>
            </div>
        </div>
        <div id="header-row">
            <div id="logo">
                <a href="/">
                    <img src="/images/avatar/me-130x130.jpg" alt="logo">
                </a>
            </div>
            <div class="header-info">
                <div id="header-title">
                    
                    <h2>
                        清风觅影
                    </h2>
                    
                </div>
                <div id="header-description">
                    
                    <h3>
                        致需极,守静笃
                    </h3>
                    
                </div>
            </div>
            <nav class="header-nav">
                <div class="social">
                    
                        <a title="Home"  href="/">
                            <i class="fa fa-home fa-2x"></i></a>
                    
                        <a title="Github" target="_blank" href="//github.com/bluarry">
                            <i class="fa fa-github fa-2x"></i></a>
                    
                        <a title="mail" target="_blank" href="mailto://bluarry@qq.com">
                            <i class="fa fa-envelope-o fa-2x"></i></a>
                    
                </div>
            </nav>
        </div>
    </div>
</header>
      <div class="outer">
        <section id="main" class="body-wrap"><article id="post-2017-08-29-线段树模板" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="post-title" itemprop="name">
      线段树模板
    </h1>
    <div class="post-title-bar">
      <ul>
          
              <li>
                  <i class="fa fa-book"></i>
                  
                      <a href="/categories/默认分类/">默认分类</a>
                  
                      <a href="/categories/学习分享/">学习分享</a>
                  
              </li>
          
        <li>
          <i class="fa fa-calendar"></i>  2017-08-29
        </li>
        <li>
          <i class="fa fa-eye"></i>
          <span id="busuanzi_value_page_pv"></span>
        </li>
      </ul>
    </div>
  

          
      </header>
    
    <div class="article-entry post-content" itemprop="articleBody">
      
            
            <p>线段树模板<br><a id="more"></a></p>
<h1>题目是poj3468,线段树的模板题.</h1>

<h2>首先来学习一波线段树是干什么的?线段树实质是一棵二叉树，因此编号为k的结点的左儿子编号为2k，右儿子编号为2k+1，它的每个结点上存放着它结点表示的区间和这个区间上的一些信息。如区间和，区间最值，区间染色.区间为[l,r]的结点的左孩子表示的区间是[l,(l+r)/2] ，右孩子表示的区间是 [(l+r)/2+1,r] 。可以发现，一个结点的左右孩子表示的区间的并集就是这个结点所表示的区间。</h2>

<p><img src="https://cdn.bluarry.top/wp-content/uploads/2019/12/sg1.jpg" alt="线段树的概念"></p>
<h1>可以发现，大致上线段树每下降一层，每个结点代表的区间长度除2，因此，线段树最多有log2(n-1)+1层。这意味着在?(????)的时间内从根访问到叶子。考虑线段树的结点数，极限情况是线段树表示的区间为[1,2^?+1]，其中有一个结点是在最右下方，这时线段树结点编号最大是 2^(?+2) −1≈4×2^k 。因此线段树应该开4倍区间长度的数组。空间复杂度是O(n)。</h1>

<h1>线段树能解决的问题包括单点修改区间查询，区间修改区间查询. 如下的hdu-1166</h1>

<h2>这是一道经典的单点修改区间查询的例子.那么有两种操作，查询，修改。</h2>

<h2>接下来介绍查询:</h2>

<h1>查询操作相当于沿着线段树的左右两条链下去，若遍历到区间[l,r]是询问区间[L,R]的子区间，那么答案加上这个区间，终止访问，否则继续访问左右子树。</h1>

<p><img src="https://cdn.bluarry.top/wp-content/uploads/2019/12/sg2.jpg" alt="区间查询例子"></p>
<h2>之前说过，链的长度是logn级别的，因此一次询问只要logn的时间，总复杂度是O(mlogn)。询问的时限问题解决了。</h2>

<h1>修改操作:</h1>

<h2>对于修改操作，也是同样的思路，沿着一条链下去，将表示这个点的区间修改完后，回溯的过程中更新包含这条链的区间和就行了。根询问一样，复杂度也是O(mlogn)。</h2>

<p><img src="https://cdn.bluarry.top/wp-content/uploads/2019/12/sg3.jpg" alt="单点修改例子"></p>
<h1>实现代码如下</h1>

<pre><code class="language-cpp line-numbers">#include &lt;iostream&gt;
#include &lt;cstdio&gt;
#include &lt;cstring&gt;
#include &lt;algorithm&gt;
using namespace std;
#define lson(x) (2*x)
#define rson(x) (2*x+1)
const int maxn=50000;
//单点修改区间查询，可用线段树解决，也可用树状数组解决
int a[maxn+5];
struct Treenode{
    int l,r,sum;
};
Treenode tree[maxn*4+5];
void build(int l,int r,int pos) {   //建立线段树
    if(l==r) {
        tree[pos].l=tree[pos].r=l;
        tree[pos].sum=a[l];
        return;
    }
    build(l,(l+r)/2,lson(pos));
    build((l+r)/2+1,r,rson(pos));
    tree[pos].l=l;tree[pos].r=r;
    tree[pos].sum=tree[lson(pos)].sum+tree[rson(pos)].sum;
}
int Querry(int l,int r,int pos){    //查询区间[l,r]之间的和
    if(l==tree[pos].l &amp;&amp; r == tree[pos].r){
        return tree[pos].sum;
    }
    int mid=(tree[pos].l+tree[pos].r)/2;
    if(r&lt;=mid) return Querry(l,r,lson(pos));
    if(l&gt;mid)  return Querry(l,r,rson(pos));

    return Querry(l,mid,lson(pos))+Querry(mid+1,r,rson(pos));
}
void Modify(int x,int y,int pos){       //修改位置为x的叶子节点+y;
    if(tree[pos].l==x &amp;&amp; tree[pos].r==x){
        tree[pos].sum+=y;
        return;
    }

    int mid=(tree[pos].l+tree[pos].r)/2;
    if(x&lt;=mid) Modify(x,y,lson(pos));
    else Modify(x,y,rson(pos));
    tree[pos].sum+=y;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int T;
    cin &gt;&gt; T;
    int cases=1;
    while(T--){
        cout &lt;&lt; "Case "&lt;&lt;cases++&lt;&lt;":" &lt;&lt;endl;
        int n;
        cin &gt;&gt; n;
        for(int i=1;i&lt;=n;i++)
            cin &gt;&gt; a[i];
        build(1,n,1);
        string s;
        int x,y;
        while(cin &gt;&gt; s)
        {
            if(s[0]=='Q')
                {cin &gt;&gt; x &gt;&gt; y;cout &lt;&lt; Querry(x,y,1)&lt;&lt;endl;}
            else if(s[0]=='A')
                {cin &gt;&gt; x &gt;&gt; y;Modify(x,y,1);}
            else if(s[0]=='S')
                {cin &gt;&gt; x &gt;&gt; y;Modify(x,-y,1);}
            else if(s[0]=='E')
                break;
        }
    }

    return 0;
}
</code></pre>

<h1>接下来就是区间修改区间查询的例子了，poj3468</h1>

<h2>这道题跟上一题的区别是上一题只要求修改一个点，而这一题要求修改一段区间。</h2>

<h2>同样还是两种操作 修改，查询(该操作和之前的没太大区别，记得更新lazy即可)</h2>

<h2>对于修改操作，类比询问操作的思想，也是沿着两条链下去修改每一段子区间，不过这里出现了一个问题：如图如果修改区间[2,9]，但是修改到[6,8]时就已经结束了，这样的话，[6,7][8,8][6,6][7,7]这四段区间并没有修改。</h2>

<p><img src="https://cdn.bluarry.top/wp-content/uploads/2019/12/sg4.jpg" alt></p>
<h2>解决方法是:</h2>

<h2>我们对于线段树上的每个结点再维护一个lazy值，如上图，假设[6,8]区间的编号为rt。当我们给[6,8]加c时，同时lazy[rt]+=c。表示有一个+c的修改还没有传下去，这样的话，当我们查询[6,7] [8,8] [6,6] [7,7]时，再将修改传下去就行了。</h2>

<h2>也就是我们将当前rt节点表示的区间值修改了，并且不在往下更新，将修改区间时每个节点修改的值记录到lazy里即可。当下次查询或者修改是我们直接把lazy传下去并且更新子节点即可.</h2>

<h1>实现代码如下:</h1>

<pre><code class="language-cpp line-numbers">#include &lt;iostream&gt;
#include &lt;cstring&gt;
#include &lt;cstdio&gt;
#include &lt;algorithm&gt;
using namespace std;
typedef long long ll;
#define lson(x) (2*x)
#define rson(x) (2*x+1)
#define QuickIO() {cin.tie(0);ios::sync_with_stdio(false);cout.tie(0);}
const ll maxn=1e5+5;

struct Treenode{
    ll l,r,sum;
};
Treenode tree[maxn*4+5];
ll lazy[maxn*4+5];
void build(ll l,ll r,ll pos){ //建立线段树
    if(l==r){
        tree[pos].l=tree[pos].r=l;
        cin &gt;&gt; tree[pos].sum;
        return;
    }
    ll mid=(l+r)/2;
    build(l,mid,lson(pos));
    build(mid+1,r,rson(pos));
    tree[pos].l=l;tree[pos].r=r;
    tree[pos].sum=tree[lson(pos)].sum+tree[rson(pos)].sum;
}
void pushdown(ll pos)
{
    ll l=tree[pos].l,r=tree[pos].r;
    ll mid=(l+r)/2;
    lazy[lson(pos)]+=lazy[pos];
    lazy[rson(pos)]+=lazy[pos];
    tree[lson(pos)].sum+=(mid-l+1)*lazy[pos];
    tree[rson(pos)].sum+=(r-mid)*lazy[pos];
    lazy[pos]=0;
}
ll Querry(ll l,ll r,ll pos){    //查询区间[l,r]之间的和
    if(l==tree[pos].l &amp;&amp; r == tree[pos].r){
        return tree[pos].sum;
    }
    if(lazy[pos])
        pushdown(pos);
    ll mid=(tree[pos].l+tree[pos].r)/2;
    if(r&lt;=mid) return Querry(l,r,lson(pos));
    if(l&gt;mid)  return Querry(l,r,rson(pos));

    return Querry(l,mid,lson(pos))+Querry(mid+1,r,rson(pos));
}
//修改,由于是区间修改，那么就只维护lazy
void Modify(ll l,ll r,ll x,ll pos){
    if(tree[pos].l==l &amp;&amp; tree[pos].r==r){
            tree[pos].sum+=x*(r-l+1);
            lazy[pos]+=x;
        return ;
    }
    if(tree[pos].l==tree[pos].r) return ;       //这条优化不加会T掉
    if(lazy[pos]){
        pushdown(pos);
    }
    ll mid=(tree[pos].l+tree[pos].r)/2;
    if(r&lt;=mid) Modify(l,r,x,lson(pos));
    else if(l&gt;mid) Modify(l,r,x,rson(pos));
    else
    {
        Modify(l,mid,x,lson(pos));
        Modify(mid+1,r,x,rson(pos));
    }
    tree[pos].sum=tree[lson(pos)].sum+tree[rson(pos)].sum;
}

ll N,Q;muban
int main()
{
    QuickIO();
    // freopen("in.txt","r",stdin);
    while(cin &gt;&gt; N &gt;&gt; Q){
        memset(lazy,0,sizeof lazy);
        build(1,N,1);
        char c;
        ll x,y,z;
        while(Q--){
            cin &gt;&gt; c;
            if(c=='Q')
            {
                cin &gt;&gt; x &gt;&gt; y;
                cout &lt;&lt; Querry(x,y,1) &lt;&lt; endl;
            }
            else if(c=='C'){
                cin &gt;&gt; x &gt;&gt; y &gt;&gt; z;
                Modify(x,y,z,1);
            }

        }
    }
    return 0;
}
</code></pre>
            <div class="post-copyright">
    <div class="content">
        <p>最后更新： 2020年03月02日 20:53</p>
        <p>原始链接： <a class="post-url" href="/2017/08/29/2017-08-29-线段树模板/" title="线段树模板">/2017/08/29/2017-08-29-线段树模板/</a></p>
        <footer>
            <a href="">
                <img src="/images/avatar/me-130x130.jpg" alt="bluarry">
                bluarry
            </a>
        </footer>
    </div>
</div>

      
        
            
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;">赏</a>
</div>

<div id="reward" class="post-modal reward-lay">
    <a class="close" href="javascript:;" id="reward-close">×</a>
    <span class="reward-title">
        <i class="icon icon-quote-left"></i>
        请我吃糖~
        <i class="icon icon-quote-right"></i>
    </span>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/images/wechat_code.jpg" alt="打赏二维码">
        </div>
        <div class="reward-select">
            
            <label class="reward-select-item checked" data-id="wechat" data-wechat="/images/wechat_code.jpg">
                <img class="reward-select-item-wechat" src="/images/wechat.png" alt="微信">
            </label>
            
            
            <label class="reward-select-item" data-id="alipay" data-alipay="/images/alipay_code.jpg">
                <img class="reward-select-item-alipay" src="/images/alipay.png" alt="支付宝">
            </label>
            
        </div>
    </div>
</div>


        
    </div>
    <footer class="article-footer">
        
        
<div class="post-share">
    <a href="javascript:;" id="share-sub" class="post-share-fab">
        <i class="fa fa-share-alt"></i>
    </a>
    <div class="post-share-list" id="share-list">
        <ul class="share-icons">
          <li>
            <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=/2017/08/29/2017-08-29-线段树模板/&title=《线段树模板》 — 探花需拔根&pic=images/avatar/me-130x130.jpg" data-title="微博">
              <i class="fa fa-weibo"></i>
            </a>
          </li>
          <li>
            <a class="weixin share-sns" id="wxFab" href="javascript:;" data-title="微信">
              <i class="fa fa-weixin"></i>
            </a>
          </li>
          <li>
            <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=/2017/08/29/2017-08-29-线段树模板/&title=《线段树模板》 — 探花需拔根&source=线段树模板" data-title="QQ">
              <i class="fa fa-qq"></i>
            </a>
          </li>
          <li>
            <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=/2017/08/29/2017-08-29-线段树模板/" data-title="Facebook">
              <i class="fa fa-facebook"></i>
            </a>
          </li>
          <li>
            <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《线段树模板》 — 探花需拔根&url=/2017/08/29/2017-08-29-线段树模板/&via=" data-title="Twitter">
              <i class="fa fa-twitter"></i>
            </a>
          </li>
          <li>
            <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=/2017/08/29/2017-08-29-线段树模板/" data-title="Google+">
              <i class="fa fa-google-plus"></i>
            </a>
          </li>
        </ul>
     </div>
</div>
<div class="post-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;" id="wxShare-close">×</a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>

<div class="mask"></div>

        
        <ul class="article-footer-menu">
            
            
  <li class="article-footer-tags">
    <i class="fa fa-tags"></i>
      
    <a href="/tags/学习/" class="color3">学习</a>
      
    <a href="/tags/acm/" class="color4">acm</a>
      
    <a href="/tags/算法/" class="color3">算法</a>
      
  </li>

        </ul>
        
        

    <aside class="post-toc-pos post-toc-top" id="post-toc">
        <nav class="post-toc-wrap">
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#null"><span class="post-toc-text">题目是poj3468,线段树的模板题.</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">首先来学习一波线段树是干什么的?线段树实质是一棵二叉树，因此编号为k的结点的左儿子编号为2k，右儿子编号为2k+1，它的每个结点上存放着它结点表示的区间和这个区间上的一些信息。如区间和，区间最值，区间染色.区间为[l,r]的结点的左孩子表示的区间是[l,(l+r)/2] ，右孩子表示的区间是 [(l+r)/2+1,r] 。可以发现，一个结点的左右孩子表示的区间的并集就是这个结点所表示的区间。</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#null"><span class="post-toc-text">可以发现，大致上线段树每下降一层，每个结点代表的区间长度除2，因此，线段树最多有log2(n-1)+1层。这意味着在?(????)的时间内从根访问到叶子。考虑线段树的结点数，极限情况是线段树表示的区间为[1,2^?+1]，其中有一个结点是在最右下方，这时线段树结点编号最大是 2^(?+2) −1≈4×2^k 。因此线段树应该开4倍区间长度的数组。空间复杂度是O(n)。</span></a></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#null"><span class="post-toc-text">线段树能解决的问题包括单点修改区间查询，区间修改区间查询. 如下的hdu-1166</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">这是一道经典的单点修改区间查询的例子.那么有两种操作，查询，修改。</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">接下来介绍查询:</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#null"><span class="post-toc-text">查询操作相当于沿着线段树的左右两条链下去，若遍历到区间[l,r]是询问区间[L,R]的子区间，那么答案加上这个区间，终止访问，否则继续访问左右子树。</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">之前说过，链的长度是logn级别的，因此一次询问只要logn的时间，总复杂度是O(mlogn)。询问的时限问题解决了。</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#null"><span class="post-toc-text">修改操作:</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">对于修改操作，也是同样的思路，沿着一条链下去，将表示这个点的区间修改完后，回溯的过程中更新包含这条链的区间和就行了。根询问一样，复杂度也是O(mlogn)。</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#null"><span class="post-toc-text">实现代码如下</span></a></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#null"><span class="post-toc-text">接下来就是区间修改区间查询的例子了，poj3468</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">这道题跟上一题的区别是上一题只要求修改一个点，而这一题要求修改一段区间。</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">同样还是两种操作 修改，查询(该操作和之前的没太大区别，记得更新lazy即可)</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">对于修改操作，类比询问操作的思想，也是沿着两条链下去修改每一段子区间，不过这里出现了一个问题：如图如果修改区间[2,9]，但是修改到[6,8]时就已经结束了，这样的话，[6,7][8,8][6,6][7,7]这四段区间并没有修改。</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">解决方法是:</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">我们对于线段树上的每个结点再维护一个lazy值，如上图，假设[6,8]区间的编号为rt。当我们给[6,8]加c时，同时lazy[rt]+=c。表示有一个+c的修改还没有传下去，这样的话，当我们查询[6,7] [8,8] [6,6] [7,7]时，再将修改传下去就行了。</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#null"><span class="post-toc-text">也就是我们将当前rt节点表示的区间值修改了，并且不在往下更新，将修改区间时每个节点修改的值记录到lazy里即可。当下次查询或者修改是我们直接把lazy传下去并且更新子节点即可.</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#null"><span class="post-toc-text">实现代码如下:</span></a></li></ol>
        </nav>
    </aside>
    

<nav id="article-nav">
  
    <a href="/2017/10/15/2017-10-15-关于ss配置安装的一些问题/" id="article-nav-newer" class="article-nav-link-wrap">

      <span class="article-nav-title">
        <i class="fa fa-hand-o-left" aria-hidden="true"></i>
        
          关于ss配置安装的一些问题
        
      </span>
    </a>
  
  
    <a href="/2017/07/22/2017-07-22-腾讯2015复试面试题--易碎的鸟蛋/" id="article-nav-older" class="article-nav-link-wrap">
      <span class="article-nav-title">腾讯2015复试面试题--易碎的鸟蛋</span>
      <i class="fa fa-hand-o-right" aria-hidden="true"></i>
    </a>
  
</nav>




    <!-- HTML页面布局 -->
    <div id="tab-list">
        <ul id="ul1">
            
            
            
            <li id="gittalk" style="width: 25%;">gittalk</li>
            
            
            <li id="valine" style="width: 25%;">valine</li>
            
        </ul>
         
        
          
        <div id="dv_gitment" class="hide">
            <div id="git_comments"></div>;
        </div>
        
        
        <div id="dv_vment" class="show">
            <div class="comments vcomment" id="vcomments"></div>
        </div>
        
    </div>

    <script src="/js/tablist.js"></script>
   

    
        
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script src="https://cdn.bluarry.top/cdn/js/md5.min.js"></script>
<!--<div id="git_comments"></div>-->
<script>
    // var gitment = new Gitment({
    //     owner: 'bluarry',
    //     repo: 'blog_comment',
    //     oauth: {
    //         client_id: '5c95f5b820d109c8f5c1',
    //         client_secret: 'f1cfe5b08f9a55923ba26a9ec8855b631002ad18',
    //     },
    // })
    var hrefs=location.href;
    var links=hrefs.split('/');
    var cr="";
    for (var i=0;i<Math.min(7,links.length);i++) {
        cr+=links[i];
    }
    console.log(cr);

    var gitalk = new Gitalk({
        clientID: '5c95f5b820d109c8f5c1', //Client ID
        clientSecret: 'f1cfe5b08f9a55923ba26a9ec8855b631002ad18', //Client Secret
        repo: 'blog_comment',//仓库名称
        owner: 'bluarry',//仓库拥有者
        admin: ['bluarry'],
        id: md5(cr),      // Ensure uniqueness and length less than 50
        distractionFreeMode: false  // Facebook-like distraction free mode
    });
    gitalk.render('git_comments');
</script>


    
        <!-- Valine Comments -->
<!-- <div class="comments vcomment" id="vcomments"></div>-->
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
<!-- Valine Comments script -->
<script>
    var GUEST_INFO = ['nick','mail','link'];
    var guest_info = 'nick,mail'.split(',').filter(function(item){
        return GUEST_INFO.indexOf(item) > -1
    });
    new Valine({
        el: '#vcomments',
        notify: 'true' == 'true',
        verify: 'false' == 'true',
        appId: "DRrkhLYTmB6QwGy6sI6vCH8C-gzGzoHsz",
        appKey: "M4A4lrKwbixQmmeO5lVdY5l2",
        avatar: "mm",
        placeholder: "少侠，留下你的评论吧",
        guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
        pageSize: "10"
    })
</script>
<!-- Valine Comments end -->

    


    </footer>
  </div>
</article>


</section>
        
      </div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info" class="inner">
      
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        <i class="fa fa-user"></i>&nbsp;&nbsp;<span id="busuanzi_value_site_uv"></span>
    </span>&nbsp;&nbsp;|&nbsp;&nbsp;
    <span id="busuanzi_container_site_pv" style='display:none'>
        <i class="fa fa-eye"></i> &nbsp;&nbsp;<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


      <p>
      &copy; 2016-2021 &nbsp;&nbsp;<i style="color: red;" class="fa fa-heart"></i>&nbsp;&nbsp; bluarry
      备案号: <a style="color: #999;" href="http://www.beian.miit.gov.cn/" target="_blank">陕ICP备2020012959号</a>
    </p>

    </div>
  </div>
</footer>
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
  var mihoConfig = {
      root: "",
      animate: true,
      isHome: false,
      share: true,
      reward: 1
  }
</script>
<div class="sidebar">
    <div id="sidebar-search" title="Search">
        <i class="fa fa-search"></i>
    </div>
    <div id="sidebar-category" title="Categories">
        <i class="fa fa-book"></i>
    </div>
    <div id="sidebar-tag" title="Tags">
        <i class="fa fa-tags"></i>
    </div>
    <div id="sidebar-top">
        <span class="sidebar-top-icon"><i class="fa fa-angle-up"></i></span>
    </div>
</div>
<div class="sidebar-menu-box" id="sidebar-menu-box">
    <div class="sidebar-menu-box-container">
        <div id="sidebar-menu-box-categories">
            <a class="category-link" href="/categories/linux/">linux</a><a class="category-link" href="/categories/linux-学习/">linux 学习</a><a class="category-link" href="/categories/linux-学习-acm-onlinejudge-hustoj/">linux 学习 acm onlinejudge hustoj</a><a class="category-link" href="/categories/学习分享/">学习分享</a><a class="category-link" href="/categories/数据结构/">数据结构</a><a class="category-link" href="/categories/经验分享/">经验分享</a><a class="category-link" href="/categories/经验分享/学习分享/">学习分享</a><a class="category-link" href="/categories/默认分类/">默认分类</a><a class="category-link" href="/categories/默认分类/复习/">复习</a><a class="category-link" href="/categories/默认分类/复习/学习分享/">学习分享</a><a class="category-link" href="/categories/默认分类/学习分享/">学习分享</a><a class="category-link" href="/categories/默认分类/生活杂谈/">生活杂谈</a><a class="category-link" href="/categories/默认分类/经验分享/">经验分享</a><a class="category-link" href="/categories/默认分类/经验分享/学习分享/">学习分享</a>
        </div>
        <div id="sidebar-menu-box-tags">
            <a href="/tags/Java/" style="font-size: 10px;">Java</a> <a href="/tags/acm/" style="font-size: 12.86px;">acm</a> <a href="/tags/android/" style="font-size: 11.43px;">android</a> <a href="/tags/c/" style="font-size: 14.29px;">c++</a> <a href="/tags/fabric/" style="font-size: 11.43px;">fabric</a> <a href="/tags/kali/" style="font-size: 10px;">kali</a> <a href="/tags/linux/" style="font-size: 15.71px;">linux</a> <a href="/tags/linux学习/" style="font-size: 10px;">linux学习</a> <a href="/tags/mac/" style="font-size: 10px;">mac</a> <a href="/tags/mfc/" style="font-size: 10px;">mfc</a> <a href="/tags/onlinejudge/" style="font-size: 10px;">onlinejudge</a> <a href="/tags/sqlite/" style="font-size: 10px;">sqlite</a> <a href="/tags/sqlite3/" style="font-size: 11.43px;">sqlite3</a> <a href="/tags/ss/" style="font-size: 10px;">ss</a> <a href="/tags/vs/" style="font-size: 11.43px;">vs</a> <a href="/tags/xposed/" style="font-size: 10px;">xposed</a> <a href="/tags/xposed模块/" style="font-size: 10px;">xposed模块</a> <a href="/tags/专业知识/" style="font-size: 10px;">专业知识</a> <a href="/tags/侧链/" style="font-size: 10px;">侧链</a> <a href="/tags/内网穿透/" style="font-size: 10px;">内网穿透</a> <a href="/tags/区块链/" style="font-size: 11.43px;">区块链</a> <a href="/tags/复习/" style="font-size: 10px;">复习</a> <a href="/tags/学习/" style="font-size: 18.57px;">学习</a> <a href="/tags/安卓/" style="font-size: 12.86px;">安卓</a> <a href="/tags/小技巧/" style="font-size: 17.14px;">小技巧</a> <a href="/tags/数据库/" style="font-size: 10px;">数据库</a> <a href="/tags/数据结构/" style="font-size: 10px;">数据结构</a> <a href="/tags/日常使用问题/" style="font-size: 10px;">日常使用问题</a> <a href="/tags/最小生成树/" style="font-size: 11.43px;">最小生成树</a> <a href="/tags/最短路径/" style="font-size: 10px;">最短路径</a> <a href="/tags/比特币/" style="font-size: 11.43px;">比特币</a> <a href="/tags/算法/" style="font-size: 12.86px;">算法</a> <a href="/tags/算法笔记/" style="font-size: 11.43px;">算法笔记</a> <a href="/tags/经验/" style="font-size: 20px;">经验</a> <a href="/tags/编译原理/" style="font-size: 10px;">编译原理</a> <a href="/tags/翻译/" style="font-size: 10px;">翻译</a> <a href="/tags/自然计算/" style="font-size: 10px;">自然计算</a>
        </div>
    </div>
    <a href="javascript:;" class="sidebar-menu-box-close">&times;</a>
</div>
<div class="mobile-header-menu-nav" id="mobile-header-menu-nav">
    <div class="mobile-header-menu-container">
        <span class="title">Menus</span>
        <ul class="mobile-header-menu-navbar">
            
            <li>
                <a  href="/">
                    <i class="fa fa-home"></i><span>主页</span>
                </a>
            </li>
            
            <li>
                <a  href="/archives">
                    <i class="fa fa-archive"></i><span>归档</span>
                </a>
            </li>
            
            <li>
                <a  href="/friends">
                    <i class="fa fa-envira"></i><span>友链</span>
                </a>
            </li>
            
            <li>
                <a  href="/about">
                    <i class="fa fa-user"></i><span>关于我</span>
                </a>
            </li>
            
        </ul>
    </div>
    <div class="mobile-header-tag-container">
        <span class="title">Tags</span>
        <div id="mobile-header-container-tags">
            <a href="/tags/Java/" style="font-size: 10px;">Java</a> <a href="/tags/acm/" style="font-size: 12.86px;">acm</a> <a href="/tags/android/" style="font-size: 11.43px;">android</a> <a href="/tags/c/" style="font-size: 14.29px;">c++</a> <a href="/tags/fabric/" style="font-size: 11.43px;">fabric</a> <a href="/tags/kali/" style="font-size: 10px;">kali</a> <a href="/tags/linux/" style="font-size: 15.71px;">linux</a> <a href="/tags/linux学习/" style="font-size: 10px;">linux学习</a> <a href="/tags/mac/" style="font-size: 10px;">mac</a> <a href="/tags/mfc/" style="font-size: 10px;">mfc</a> <a href="/tags/onlinejudge/" style="font-size: 10px;">onlinejudge</a> <a href="/tags/sqlite/" style="font-size: 10px;">sqlite</a> <a href="/tags/sqlite3/" style="font-size: 11.43px;">sqlite3</a> <a href="/tags/ss/" style="font-size: 10px;">ss</a> <a href="/tags/vs/" style="font-size: 11.43px;">vs</a> <a href="/tags/xposed/" style="font-size: 10px;">xposed</a> <a href="/tags/xposed模块/" style="font-size: 10px;">xposed模块</a> <a href="/tags/专业知识/" style="font-size: 10px;">专业知识</a> <a href="/tags/侧链/" style="font-size: 10px;">侧链</a> <a href="/tags/内网穿透/" style="font-size: 10px;">内网穿透</a> <a href="/tags/区块链/" style="font-size: 11.43px;">区块链</a> <a href="/tags/复习/" style="font-size: 10px;">复习</a> <a href="/tags/学习/" style="font-size: 18.57px;">学习</a> <a href="/tags/安卓/" style="font-size: 12.86px;">安卓</a> <a href="/tags/小技巧/" style="font-size: 17.14px;">小技巧</a> <a href="/tags/数据库/" style="font-size: 10px;">数据库</a> <a href="/tags/数据结构/" style="font-size: 10px;">数据结构</a> <a href="/tags/日常使用问题/" style="font-size: 10px;">日常使用问题</a> <a href="/tags/最小生成树/" style="font-size: 11.43px;">最小生成树</a> <a href="/tags/最短路径/" style="font-size: 10px;">最短路径</a> <a href="/tags/比特币/" style="font-size: 11.43px;">比特币</a> <a href="/tags/算法/" style="font-size: 12.86px;">算法</a> <a href="/tags/算法笔记/" style="font-size: 11.43px;">算法笔记</a> <a href="/tags/经验/" style="font-size: 20px;">经验</a> <a href="/tags/编译原理/" style="font-size: 10px;">编译原理</a> <a href="/tags/翻译/" style="font-size: 10px;">翻译</a> <a href="/tags/自然计算/" style="font-size: 10px;">自然计算</a>
        </div>
    </div>
</div>
<div class="search-wrap">
    <span class="search-close">&times;</span>
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
            <i class="icon icon-lg icon-chevron-left"></i>
        </a>
        <input class="search-field" placeholder="Search..." id="keywords">
        <a id="search-submit" href="javascript:;">
            <i class="fa fa-search"></i>
        </a>
    <div class="search-container" id="search-container">
        <ul class="search-result" id="search-result">
        </ul>
    </div>
</div>

<div id="search-tpl">
    <li class="search-result-item">
        <a href="{url}" class="search-item-li">
            <span class="search-item-li-title" title="{title}">{title}</span>
        </a>
    </li>
</div>
<script src="/js/search.js"></script>
<script src="/js/main.js"></script>


  <script src="//cdn.bootcss.com/particles.js/2.0.0/particles.min.js"></script>
  <div id="particles"></div>
  <script src="/js/particles.js"></script>







  <link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.5.0/animate.min.css">
  <script src="//cdn.bootcss.com/scrollReveal.js/3.0.5/scrollreveal.js"></script>
  <script src="/js/animate.js"></script>


  <script src="/js/pop-img.js"></script>
  <script>
     $(".article-entry p img").popImg();
  </script>

  </div>
</body>
</html>